﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Projeto_Build_0.Models;
using Projeto_Build_0.ViewModels;

namespace Projeto_Build_0.Controllers
{
    public class SemanaEscolarController : Controller
    {
        private JardimEntities db = new JardimEntities();

        //
        // GET: /SemanaEscolar/

        [Authorize(Roles = "Funcionario, EE, Administrador")]
        public ViewResult Index(int Nsemana, int Ano)
        {

            //((DateTime.Now.DayOfYear+6)/7);

            SemanaEscolar semana = (from p in db.SemanaEscolars.ToList()
                                    where (p.NumeroSemana == Nsemana && p.Ano == Ano)
                                    select p).FirstOrDefault();

            Evento[] lista = (from p in db.Eventos.ToList()
                              where (p.DiaEscolar.SemanaEscolar == semana)
                              select p).ToArray();



            return View(lista);
        }

        [Authorize(Roles = "Funcionario, Administrador")]
        public ActionResult CreateEvento()
        {

            return RedirectToAction("Create", "Evento");   
        }

          [Authorize(Roles = "Funcionario, EE, Administrador")]
        public ViewResult Ementa(int semana)
        {
            

            DiaEscolar[] lista1 = (from p in db.DiaEscolars.ToList()
                              where (p.SemanaEscolar.SemanaEscolarId == semana)
                              select p).ToArray();

            ListaPratos[] lista2 = (from p in db.ListaPratos.ToList()
                                    from a in lista1
                                    where a.SemanaEscolar.SemanaEscolarId == semana
                                    &&  p.Ementa.EmentaId == a.Ementa.EmentaId
                                    select p).ToArray();
              
            var viewModel = new EmentaViewModel { DiasEscolares = lista1, ListaPratos = lista2 };


            return View(viewModel);
        }

    }
}